################################
###        MIT License       ###
################################

import requests
import time
import json
import hashlib

# 接口地址  测试帐号：13000000008  密码：888999
baseUrl = 'https://xt.dzd.com:8060'

# 帐户KEY
apiKey = '267013233c62edd1255fdc7342f314df'

# 帐户ID
accountId = '168'

# 提交到接口方法
def post(url, data):
    headers = {
        'content-type': 'application/json'
    }
    r = requests.post(baseUrl + url, data=json.dumps(data), headers=headers)
    return r.text


# token 加密后的字符串，值为：MD5(股票帐户ID+当前时间戳+KEY)
def deal_with_params(data, params=[]):
    data['token'] = data['accountId'] + str(data['timestramp']) + apiKey
    for key in params:
        data['token'] += data[key]
    data['token'] = hashlib.md5(data['token'].encode('utf-8')).hexdigest()
    return data


# 获取当前帐户信息
def banlence():
    timestramp = int(time.time())
    result = post('/sa/banlence', deal_with_params({
        "accountId": accountId,
        "timestramp": timestramp
    }))
    return result

# 查询持仓信息
def postions():
    timestramp = int(time.time())
    result = post('/sa/postions', deal_with_params({
        "accountId": accountId,
        "timestramp": timestramp
    }))
    return result

# 买入股票
def buy():
    timestramp = int(time.time())
    result = post('/sa/buy', deal_with_params({
        "accountId": accountId,
        "timestramp": timestramp,
        "code": "000756",
        "price": "1.00",
        "amount": "100"
    },params=["code" ,"price","amount"]))

    return result

if __name__ == '__main__':
    # 获取当前帐户信息
    print(banlence())
    # 查询持仓信息
    print(postions())
    # 买入股票
    print(buy())